# Create your views here.
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.template import RequestContext
from django import forms
import string
from datetime import datetime, timedelta
from django.views.decorators.http import require_http_methods
from django.http import HttpResponse
from django.utils import simplejson
from django.core.urlresolvers import reverse
from django.contrib.sitemaps import Sitemap
from blog.models import Post, Comment

def index(request):
	post = Post.objects.all().order_by('-id')
	if (request.user.is_staff):
		valid = True
	else:
		valid = False
	context = {
		'post' : post,
		'valid' : valid,
	}
	return render_to_response(
		'blog/view_all.html', 
		context, 
		context_instance = RequestContext(request)
	)
def view_post(request, post_id, year, month, day):
	try:
		post = Post.objects.get(pk=post_id)
	except Post.DoesNotExist:
		context = {
			'post_id' : post_id,
			'year' : year,
			'month' : month,
			'day' : day,
		}
		return render_to_response('blog/404.html', context, context_instance = RequestContext(request))		
	myYear = str(post.creation_date.year)
	myMonth = str(post.creation_date.month)
	myDay = str(post.creation_date.day)
	year_s = str(myYear)
	if (year == myYear and month == myMonth and day == myDay):
		comment = Comment.objects.all().filter(post=post_id)
		if (request.user.is_staff):
			valid = True
		else:
			valid = False
		context = {
			'post' : post,
			'valid' : valid,
			'comment' : comment,
			'year' : year,
			'month' : month,
			'day' : day,
		}
		return render_to_response(
			'blog/view_post.html', 
			context, 
			context_instance = RequestContext(request)
		)
	else:
		context = {
			'post_id' : post_id,
			'myYear' : myYear,
			'myMonth' : myMonth,
			'myDay' : myDay,
			'year' : year,
			'month' : month,
			'day' : day,
			'post' : post,
		}
		return render_to_response('blog/format404.html', context, context_instance = RequestContext(request))
	
def add_comment(request, post_id, xhr="WTF?" ):
	name = request.POST["name"]
	email = request.POST["email"]
	content = request.POST["content"]
	from pykismet import *
	ak = Akismet('07789caeabcd', 'http://c-pj.net/blog/')
	if_spam = Comment('10.0.42.1', 'Lynx', author=name, email=email, content=content)
	from blog.models import Comment
	try:
		post = Post.objects.get(pk=post_id)
	except Post.DoesNotExist:
		return render_to_response('blog/404.html', {'post_id':post_id}, context_instance = RequestContext(request))
	if(if_spam.is_spam(ak)):
		errors = "Spam"
	else:
		comment = Comment(name=name, email=email, content=content, creation_date=datetime.now(), post_id=post_id)
		comment.save()
	results = {'success':True}
	json = simplejson.dumps(results)
	if request.is_ajax:
		try:
			test = True
			#return HttpResponse("{'success': 'Post was saved'}")
		except Comment.DoesNotExist:
			# This Should Never Happen!	
			error_msg = "Comment failed to save"
	return HttpResponseRedirect("http://c-pj.net/blog/" + str(post.creation_date.year) + "/" + str(post.creation_date.month) + "/" + str(post.creation_date.day) + "/" + str(post_id) + "/")

def remove_comment(request, post_id, comment_id):
	try:
		post = Post.objects.get(pk=post_id)
		comment = Comment.objects.get(pk=comment_id)
	except Comment.DoesNotExist:
		return HttpResponseRedirect("http://c-pj.net/blog/")
	if (request.user.is_staff):
		comment.delete()
	return HttpResponseRedirect("http://c-pj.net/blog/" + str(post.creation_date.year) + "/" + str(post.creation_date.month) + "/" + str(post.creation_date.day) + "/" + str(post_id) + "/")
		
